package com.luoqy.speedy.modular.web.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Async;

import com.luoqy.speedy.modular.web.model.Fiction;
import com.luoqy.speedy.modular.web.service.WriteFictionService;

public class FictionUtil {
	   /*
     * 获取 书趣阁小说基本信息
     *
     * @param document
     * @return document
     */
    public static Fiction getFictions(Document document) {
        try {
            String fictionName = document.select("meta[property=og:novel:book_name]").attr("content");
//          String createDate = document.select("meta[property=og:novel:update_time]").attr("content");
            String brief = document.select("meta[property=og:description]").attr("content");
            //String brief=s.substring(0,85);
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String type = document.select("meta[property=og:novel:category]").attr("content");
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("div>img").attr("abs:src");

            Elements small = document.select("div.small");//小说基本信息
            String number = JsoupUtil.sub(small.get(0).child(3).text());//字数
            return new Fiction(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()),"", img, brief, fictionName, author, type, newest, state, number);
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
        
    }
    /**
     * 从书架删除用户添加的书
     * @param ids 书架id集合
     * @param id 小说id
     * @return 从新拼接
     */
    public static String removeOne(String ids, String id) {
        String string = "";
        String[] result = ids.split(",");
        for (String s : result) {
            if (!s.equals(",")) {
                if (!s.equals(id)) {
                    if (string.length() > 0) {
                        string = string.concat("," + s);
                    } else {
                        string = string.concat(s);
                    }
                }
            }
        }
        return string;
    }

    /**
     * 添加书籍到书架
     * @param ids 书架id
     * @param id 添加的id
     * @return 书架id集合
     */
    public static String addOne(String ids, String id) {
        if (ids.equals("")) {
            ids = id;
        } else {
            ids = ids.concat("," + id);
        }
        return ids;
    }
}
